package com.chapter_3;

import java.util.Random;

/**
 * @Author: Timo
 * @Date: 2019/5/14 17:20
 * @Description: 服务器线程，主要用于处理客户端发送过来的请求。
 */
public class ServerThread extends Thread{
    /**
     * 客户端发送过来的所有请求
     */
    private final RequestQueue requestQueue;
    private final Random random;


    public ServerThread(RequestQueue requestQueue, long seen,String name) {
        super(name);
        this.requestQueue = requestQueue;
        this.random = new Random(seen);
    }

    /**
     * 处理客户端的所有请求
     */
    @Override
    public void run() {
        for (int i = 0; i < 10000; i++) {
            Request request = requestQueue.getRequest();
            System.out.println("<<<<<<<<<<"+Thread.currentThread().getName() + " handles " + request);
            try {
                Thread.sleep(random.nextInt(10000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
